export const sendRequest = async (
	config: OpenAPIConfig,
	options: ApiRequestOptions,
	url: string,
	body: any,
	formData: FormData | undefined,
	headers: Headers,
	onCancel: OnCancel
): Promise<XMLHttpRequest> => {
	const xhr = new XMLHttpRequest();
	xhr.open(options.method, url, true);
	xhr.withCredentials = config.WITH_CREDENTIALS;

	headers.forEach((value, key) => {
		xhr.setRequestHeader(key, value);
	});

	return new Promise<XMLHttpRequest>((resolve, reject) => {
		xhr.onload = () => resolve(xhr);
		xhr.onabort = () => reject(new Error('Request aborted'));
		xhr.onerror = () => reject(new Error('Network error'));
		xhr.send(body ?? formData);

		onCancel(() => xhr.abort());
	});
};
